#include "net.hpp"

#include "dispatcher.hpp"
void rpcRequest(const myrpc::BaseConnection::ptr& conn,myrpc::RpcRequest::ptr& msg)
{
    std::cout<<"收到一条rpc请求"<<"："<<msg->method()<<std::endl;
    std::string body=msg->serialize();
    std::cout<<body<<std::endl;

    auto rpc_rsp=myrpc::MessageFactory::create<myrpc::RpcResponse>();
    rpc_rsp->setId("1");
    rpc_rsp->setMType(myrpc::MType::RSP_RPC);
    rpc_rsp->setRcode(myrpc::RCode::RCODE_OK);
    rpc_rsp->setResult(7);
    conn->send(rpc_rsp);
}
void topicRequest(const myrpc::BaseConnection::ptr& conn,myrpc::TopicRequest::ptr& msg)
{
    std::cout<<"收到一条主题请求"<<std::endl;
    std::string body=msg->serialize();
    std::cout<<body<<std::endl;

    auto topic_rsp=myrpc::MessageFactory::create<myrpc::TopicResponse>();
    topic_rsp->setId("2");
    topic_rsp->setMType(myrpc::MType::RSP_TOPIC);
    topic_rsp->setRcode(myrpc::RCode::RCODE_OK);
    conn->send(topic_rsp);

}
int main()
{
    auto dispatcher=std::make_shared<myrpc::Dispatcher>();
    auto server=myrpc::ServerFactory::create(9090);
    dispatcher->registerHandler<myrpc::RpcRequest>(myrpc::MType::REQ_RPC,rpcRequest);
    dispatcher->registerHandler<myrpc::TopicRequest>(myrpc::MType::REQ_TOPIC,topicRequest);
    auto message_cb=std::bind(&myrpc::Dispatcher::onMessage,dispatcher.get(),std::placeholders::_1,std::placeholders::_2);
    server->setMessageCallback(message_cb);
    server->start();
    return 0;
}